package com.ellecity06.common.widgets

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import android.view.MotionEvent
import com.ellecity06.common.utils.logD
import kotlinx.coroutines.NonDisposableHandle.parent
import kotlin.math.abs

/**
 * @author zavier
 * @time 2024/10/23 9:46
 * @des 横向滑动的recyclerview
 */
class HorizontalRecyclerView @JvmOverloads constructor(context: Context, attrs: AttributeSet?=null, defStyle: Int = 0) :
    RecyclerView(context, attrs, defStyle) {


    private var mDownX = 0
    private var mDownY = 0
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                mDownX = ev.x.toInt()
                mDownY = ev.y.toInt()
            }
            MotionEvent.ACTION_MOVE -> {
                val moveX = ev.x.toInt()
                val moveY = ev.y.toInt()
                logD("moveX=${abs(moveX - mDownX)},,mDownX=${moveY - mDownY}")
                if (abs(moveX - mDownX) != 0 && abs(moveY - mDownY) != 0) {
                    if (abs(moveX - mDownX) > abs(moveY - mDownY) || abs(moveY - mDownY) < 10) {
                        parent.requestDisallowInterceptTouchEvent(true)
                    } else {
                        parent.requestDisallowInterceptTouchEvent(false)
                    }
                    mDownX = moveX
                    mDownY = moveY
                }
            }
            MotionEvent.ACTION_UP -> parent.requestDisallowInterceptTouchEvent(false)
        }
        return super.dispatchTouchEvent(ev)
    }
}